#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "blockingqueue.hpp"

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

using namespace std;

void *producer(void *arg) // 生产者生产数据
{
    BLQueue<int> *blqueue = static_cast<BLQueue<int> *>(arg);
    int data = 0;
    while (true)
    {
        blqueue->Push(data);
        {
            SmartMutex smartmutex(&mutex);
            cout << "生产者生产数据: " << data++ << endl;
        }
        sleep(1);
    }

    return nullptr;
}

void *consumer(void *arg) // 消费者消费数据
{
    BLQueue<int> *blqueue = static_cast<BLQueue<int> *>(arg);
    // sleep(1);
    int data;
    while (true)
    {
        blqueue->Pop(&data);
        {
            SmartMutex smartmutex(&mutex);
            cout << "消费者消费数据: " << data << endl;
        }
        // sleep(1);
    }

    return nullptr;
}

int main()
{
    BLQueue<int> *blqueue = new BLQueue<int>();
    pthread_t t1, t2;
    pthread_create(&t1, nullptr, producer, (void *)blqueue);
    pthread_create(&t2, nullptr, consumer, (void *)blqueue);

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    return 0;
}